﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Eis51.CampanyCMS.Model;
 
using System.Data.OleDb;
using System.Configuration;
namespace Eis51.CampanyCMS.Business
{
    public abstract class BaseBLL<T>
    {
        protected string connectionString = string.Format(ConfigurationSettings.AppSettings["Eis_ConnectionString"], System.Web.HttpContext.Current.Request.PhysicalApplicationPath);

        /// <summary>
        /// 执行SQL方法
        /// </summary>
        /// <param name="sql"></param>
        public void Execute(string sql)
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                try
                {
                    string sqlCommand = sql;
                    using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                    {
                        connection.Open();
                        cmd.ExecuteNonQuery();
                    }
                }
                catch (Exception ex)
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 执行SQL 取得Table
        /// </summary>
        /// <param name="sql">sql</param>
        /// <returns>DataTable</returns>
        public DataTable GetDataTable(string sql)
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                try
                {
                    string sqlCommand = sql;
                    using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                    {
                        connection.Open();
                        OleDbDataAdapter adp = new OleDbDataAdapter(sqlCommand, connection);
                        adp.SelectCommand = cmd;
                        DataSet ds = new DataSet();
                        adp.Fill(ds);
                        return ds.Tables[0];
                    }
                }
                catch (Exception ex)
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                    return null;
                }

            }
        }
		 
    }
}